﻿@Code
    ' Cache the image for a minute
    Response.OutputCache(60)

    Dim id = UrlData(0).AsInt()
    Dim db = Database.Open("CalibreDB")
    Dim path = db.QuerySingle("SELECT path FROM books WHERE books.id = @0", id)
    If path Is Nothing Then
        Response.SetStatus(HttpStatusCode.NotFound)
        Return
    End If
    Dim f = UrlData(1)

    
    Dim CalibreLib As String = ConfigurationManager.AppSettings("CalibreLib").ToString
    Dim strpath = CalibreLib & path.path.ToString()
    strpath = Replace(strpath, "/", "\")
    Dim name = db.QuerySingle("SELECT name FROM data WHERE book = @0", id)
    Dim filename = name.name.ToString & "." & f.ToString.ToLower

    strpath = strpath & "\" & filename
    Context.Response.ContentType = "application/" & f
   
    Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", HttpUtility.UrlPathEncode(filename)))
    
End Code